Linux

您所在的位置:网站首页 linux 管道 Linux

Linux

2022-12-20 15:41| 来源: 网络整理| 查看: 265

1. 输入与输出 标准输入 STDIN 文件描述符:0,默认:键盘输入 标准输出 STDOUT 文件描述符:1,默认:屏幕输出 错误输出 STDERR 文件描述符:2,默认:屏幕输出 2. 标准输出重定向

覆盖输出 >

追加输出 >>

注意:shell的内嵌命令set可以设置是否允许输出重定向至已存在的文件

set -C:禁止输出重定向至已存在的文件 set +C:允许输出重定向至已存在的文件

示例:标准输出重定向到文件(实际动作:先创建文件,再向其中写入标准输出内容)

[root@VM_41_201_centos ~]# ls -m anaconda-ks.cfg, sh [root@VM_41_201_centos ~]# ls -m > ls.txt [root@VM_41_201_centos ~]# cat ls.txt anaconda-ks.cfg, ls.txt, sh [root@VM_41_201_centos ~]# ls -m >> ls.txt [root@VM_41_201_centos ~]# cat ls.txt anaconda-ks.cfg, ls.txt, sh anaconda-ks.cfg, ls.txt, sh [root@VM_41_201_centos ~]# 3. 错误输出重定向

覆盖输出 2>

追加输出 2>>

示例:错误输出重定向到文件

[root@VM_41_201_centos ~]# lss 2> ls.error [root@VM_41_201_centos ~]# cat ls.error -bash: lss: 未找到命令 [root@VM_41_201_centos ~]# lss 2>> ls.error [root@VM_41_201_centos ~]# cat ls.error -bash: lss: 未找到命令 -bash: lss: 未找到命令 [root@VM_41_201_centos ~]# 4. 合并标准输出与错误输出重定向 覆盖输出 &> COMMAND &> 文件 COMMAND > 文件 2>&1 #不推荐这种形式,难记,不好理解 COMMAND > 文件A 2> 文件B 追加输出 &>> COMMAND &>> 文件 COMMAND >> 文件 2>&1 #不推荐这种形式,难记,不好理解 COMMAND >> 文件A 2>> 文件B

示例:合并标准输出与错误输出重定向

# 标准输出 [root@VM_41_201_centos ~]# ls -m anaconda-ks.cfg, ls.error, ls.txt, sh # 标准输出重定向到文件 [root@VM_41_201_centos ~]# ls -m &> ls.txt [root@VM_41_201_centos ~]# cat ls.txt anaconda-ks.cfg, ls.error, ls.txt, sh # 错误输出重定向到文件(追加) [root@VM_41_201_centos ~]# lss &>> ls.txt [root@VM_41_201_centos ~]# cat ls.txt anaconda-ks.cfg, ls.error, ls.txt, sh -bash: lss: 未找到命令 # 合并标准输出与错误输出(标准输出,覆盖) [root@VM_41_201_centos ~]# ls -m > ls.txt 2>&1 [root@VM_41_201_centos ~]# cat ls.txt anaconda-ks.cfg, ls.error, ls.txt, sh # 合并标准输出与错误输出(错误输出,覆盖) [root@VM_41_201_centos ~]# lss > ls.txt 2>&1 [root@VM_41_201_centos ~]# cat ls.txt -bash: lss: 未找到命令 # 合并标准输出与错误输出(错误输出,追加) [root@VM_41_201_centos ~]# lsss >> ls.txt 2>&1 [root@VM_41_201_centos ~]# cat ls.txt -bash: lss: 未找到命令 -bash: lsss: 未找到命令 # 合并标准输出与错误输出(错误输出,追加 ,另一种方式) [root@VM_41_201_centos ~]# lsls &>> ls.txt [root@VM_41_201_centos ~]# cat ls.txt -bash: lss: 未找到命令 -bash: lsss: 未找到命令 -bash: lsls: 未找到命令 [root@VM_41_201_centos ~]# #使用COMMAND > 文件A 2> 文件B、COMMAND >> 文件A 2>> 文件B的形式 [root@VM_41_201_centos ~]# ls anaconda-ks.cfg ls.error ls.txt sh tee.txt [root@VM_41_201_centos ~]# ls -m > A 2> B [root@VM_41_201_centos ~]# ls -m A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt [root@VM_41_201_centos ~]# cat A A, anaconda-ks.cfg, B, ls.error, ls.txt, sh, tee.txt [root@VM_41_201_centos ~]# cat B [root@VM_41_201_centos ~]# sl -m > A 2> B [root@VM_41_201_centos ~]# cat A [root@VM_41_201_centos ~]# cat B -bash: sl: 未找到命令 [root@VM_41_201_centos ~]# lss -m >> A 2>> B [root@VM_41_201_centos ~]# cat B -bash: sl: 未找到命令 -bash: lss: 未找到命令 [root@VM_41_201_centos ~]# 5. 输入重定向

输入重定向相比较而言,就比较简单了,而且用得相对较少

一般用法:将文件作为输入重定向到命令

命令 < 文件

示例:将文件输入重定向至命令wc统计文件行数:

[root@VM_41_201_centos ~]# wc -l < anaconda-ks.cfg 148 [root@VM_41_201_centos ~]# 6. 管道

管道用于连接多个命令(程序),将前一个命令的结果重定向,作为后一个命令的输入

COMMAND1 | COMMAND2 | COMMAND3 | ...

示例:将文件内容通过管道重定向到命令(其效果类似输入重定向)

[root@VM_41_201_centos ~]# cat anaconda-ks.cfg | wc -l 148 [root@VM_41_201_centos ~]# 7. 相关命令

以下几个命令经常与IO重定向(> >> 2> 2>> &> &>> )及管道(|)结合使用

7.1 tee 从标准输入读取,写入标准输出和文件

tee命令比较特殊:从标准输入读取、写入标准输出、写入文件(同时干了3件事,一箭三雕)。

示例

tee命令执行结果既输出到了指定文件,又输出到了terminal

[root@VM_41_201_centos ~]# ls anaconda-ks.cfg ls.error ls.txt sh [root@VM_41_201_centos ~]# ls -m | tee tee.txt anaconda-ks.cfg, ls.error, ls.txt, sh [root@VM_41_201_centos ~]# cat tee.txt anaconda-ks.cfg, ls.error, ls.txt, sh [root@VM_41_201_centos ~]# 7.2 tr - 替换或删除字符

语法

tr [OPTION]... SET1 [SET2]

选项

-c 使用SET1的补集; -d 删除SET1中的字符; -s 把连续重复的字符以单独一个字符表示; -t 替换SET1中SET2长度的字符。

示例

# 将大写字母替换成小写字母 [root@localhost ~]# echo "Hello World" | tr 'A-Z' 'a-z' hello world # 删除大写字母 [root@localhost ~]# echo "Hello World" | tr -d [:upper:] ello orld # 替换部分字符 [root@localhost ~]# echo "Hello World" | tr -t 'Hello' 'xx' xxllo World 7.3 输入重定向分界符:


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3